<template>
  <el-dialog :id="target" :close-on-click-modal="closeonclickmodal" width="600px" :title="getdialogTitle()"
             @open="listenopen()"
             @close="close" :visible="dialogshow">
    <el-form :model="dialoginfo" ref="dialoginfo" label-position="left">
      <el-row class="t-center">
        <el-col>
          <el-form-item>
            <el-upload  id="imageUpload"
              action="string"
              :show-file-list="false"
              list-type="picture"
              :before-upload="beforeAvatarUpload"
              :on-remove="handleRemove"
              :on-preview="handlePictureCardPreview"
              :http-request="submitUpload">
              <img v-if="dialoginfo.thumbNailImage" :src="this.proAPI.RESOURCE_URL+dialoginfo.thumbNailImage" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <!--<el-form-item :rules="[{ required: true, message: '不能为空'}]"-->
                        <!--prop="userName" label="账号" :label-width="formLabelWidth">-->
            <!--<el-input v-if="isadd" v-model.trim="dialoginfo.userName" auto-complete="off" clearable/>-->
            <!--<el-input v-else disabled v-model.trim="dialoginfo.userName" auto-complete="off"/>-->
          <!--</el-form-item>-->
          <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                        prop="qq" label="QQ" :label-width="formLabelWidth">
            <el-input type="text" v-model.trim="dialoginfo.qq" auto-complete="off" clearable></el-input>
          </el-form-item>
          <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                        prop="nickName" label="昵称" :label-width="formLabelWidth">
            <el-input type="text" v-model.trim="dialoginfo.nickName" auto-complete="off" clearable></el-input>
          </el-form-item>
          <el-form-item id="userage" :rules="[{ required: true, message: '不能为空'}]"
                        prop="age" label="年龄" :label-width="formLabelWidth">
            <el-input-number style="text-align: left" v-model.trim.number="dialoginfo.age"
                             :controls="0==1" :min="1" :max="99" auto-complete="off" clearable></el-input-number>
          </el-form-item>
          <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                        prop="email" label="Email" :label-width="formLabelWidth">
            <el-input type="text" v-model.trim="dialoginfo.email" auto-complete="off" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="2">&nbsp;</el-col>
        <el-col :span="11">
          <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                        prop="mobile" label="手机号" :label-width="formLabelWidth">
            <el-input type="text" v-model.trim="dialoginfo.mobile" auto-complete="off" clearable></el-input>
          </el-form-item>
          <el-form-item :rules="[
                      { required: true, message: '必须选择一项'}
                    ]" prop="sex" label="性别" :label-width="formLabelWidth">
            <el-select v-model.trim="dialoginfo.sex" placeholder="请选择">
              <el-option label="男" :value="1"></el-option>
              <el-option label="女" :value="2"></el-option>
              <el-option label="保密" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                        prop="address" label="地址" :label-width="formLabelWidth">
            <el-input type="text" v-model.trim="dialoginfo.address" auto-complete="off" clearable></el-input>
          </el-form-item>
          <el-form-item :rules="[{ required: true, message: '不能为空'}]"
                        prop="desc" label="备注" :label-width="formLabelWidth">
            <el-input type="text" v-model.trim="dialoginfo.desc" auto-complete="off" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item style="text-align: right">
          <el-button size="mini" @click="dialogshow = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="toAdd('dialoginfo')">保 存</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
  export default {
    inject: ["reload"],
    data() {
      return {
        target: "userBaseInfoEdit",
        fileList: [],
        dialogImageUrl: '',
        dialogVisible: false,
        isajax: false,
        imageUrl: '',
        closeonclickmodal: true,
        formLabelWidth: '70px',
        headers: {
          'CSRFToken': this.$store.state.CSRFToken
        }
      }
    },
    props: ['dialogshow', 'dialoginfo', 'isadd'],
    methods: {
      getdialogTitle() {
        return this.isadd == true ? "新增账号基本资料" : "修改账号基本资料";
      },
      listenopen() {
        if (this.isadd && this.$refs['dialoginfo'] != null) {
          this.$refs['dialoginfo'].resetFields();
        }
        // console.log(this.isadd);
        // console.log(this.dialogshow);
      },
      close() {
        this.$emit('changedialogshow')
      },
      toAdd(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.commit("saveLoadingMap", {"target": this.proAPI.URLS.modifySysUserBaseInfoByUserId,id:this.target});
            this.proAPI.postJson(this.proAPI.URLS.modifySysUserBaseInfoByUserId, this.dialoginfo).then((res) => {
              this.$message.success({
                "message": res.message
              })
              this.reload();
            });
          } else {
            return false;
          }
        });
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isPNG = file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG&&!isPNG) {
          this.$message.error('图片格式只能是 JPG或者PNG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('图片大小不能超过 2MB!');
        }
        return (isJPG||isPNG) && isLt2M;
      },
      submitUpload(item) {
        // console.log(item);
        let formData = new FormData()
        formData.append('file', item.file)
        this.$store.commit("saveLoadingMap", {"target": this.proAPI.URLS.uploadSingleHeadImage,id:this.target});
        this.proAPI.postFile(this.proAPI.URLS.uploadSingleHeadImage, formData).then(res => {
          this.dialoginfo.thumbNailImage = res.data.thumbNailImagePath;
          this.dialoginfo.headImage = res.data.imagePath;
        })
      }
    },
    updated() {
      // console.log(333);
    },
    mounted() {
      // console.log("开始创建！！！！！！！！！！！！！！！！！");
    }
  };
</script>
<style scoped>
  #userage >>> .el-input-number .el-input__inner {
    text-align: left;
  }
  #imageUpload >>> .el-upload--picture{
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    border-radius: 6px;
    box-sizing: border-box;
    width: 64px;
    height: 64px;
    line-height: 64px;
    vertical-align: top;
  }

  #userBaseInfoEdit >>> .el-dialog__body {
    padding: 15px 20px;
    color: #606266;
    font-size: 14px;
  }
</style>
